package com.ajk.server.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ajk.server.controller.access.ArgIndexForPrintLog;
import com.ajk.server.exception.AjkException;
import com.ajk.server.exception.ErrorConstant;
import com.ajk.server.model.DepartmentDict;
import com.ajk.server.model.HospitalDict;
import com.ajk.server.model.response.ResultList;
import com.ajk.server.model.response.ResultObject;
import com.ajk.server.service.DepartmentDictService;
import com.ajk.server.service.HospitalDictService;
import com.ajk.server.service.RegionDictService;

/**
 * 医院 
 *
 */
@RequestMapping("/hospital")
@RestController
public class HospitalController {
	Logger logger = LoggerFactory.getLogger(HospitalController.class);

	@Autowired
	HospitalDictService hospitalDictService;

	@Autowired
	DepartmentDictService departmentDictService;

	@Autowired
	RegionDictService regionDictService;

	/**
	 *科室列表
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/department/fetch_all", method = RequestMethod.GET)
	public ResultList<DepartmentDict> fetchAllDepartment(){
		List<DepartmentDict> departments = departmentDictService.findAllWithCache();
		return new ResultList<>(departments);
	}

	/**
	 *根据parentId科室列表
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/department/by_parent_id/{parentId}", method = RequestMethod.GET)
	public ResultList<DepartmentDict> fetchByParentId(@PathVariable("parentId") Integer parentId){
		List<DepartmentDict> departments = departmentDictService.findChildrenByParentId(parentId);
		return new ResultList<>(departments);
	}

	/**
	 * 根据ID查询科室
	 * @param id
	 * @return
	 */
	@ArgIndexForPrintLog(indexes={})
	@RequestMapping(value = "/department/by_id/{id}", method = RequestMethod.GET)
	public ResultObject<DepartmentDict> fetchById(@PathVariable("id") Integer id){
		DepartmentDict department = departmentDictService.findByIdWithCache(id);
		return new ResultObject<>(department);
	}
	/**
	 *根据parentId科室列表
	 */
	@ArgIndexForPrintLog(indexes={0})
	@RequestMapping(value = "/hospital/nearby", method = RequestMethod.GET)
	public ResultList<HospitalDict> nearbyHospital(HospitalDict hospitalDict){
		if(hospitalDict.getLongitude() == null){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"经度不能为空");
		}
		if(hospitalDict.getLatitude() == null){
			throw new AjkException(ErrorConstant.PARAM_ERROR.getErrorCode(),"纬度不能为空");
		}
		List<HospitalDict> list = hospitalDictService.nearbyHospital(hospitalDict);
		
		return new ResultList<>(list);
	}
	

}